import { _decorator, Component } from 'cc';
import { EvolutionNode } from './EvolutionNode';
import { GameEvents } from '../constants/GameConstants';
import { EventManager } from '../core/EventManager';

const { ccclass, property } = _decorator;

/**
 * 陆地生物进化树
 * 
 * 功能：
 * 1. 定义陆地生物进化路线
 * 2. 管理进化节点解锁
 * 3. 处理进化条件检查
 * 4. 提供进化效果实现
 */
@ccclass('LandEvolutionTree')
export class LandEvolutionTree extends Component {
    private evolutionNodes: Map<string, EvolutionNode> = new Map();
    private currentStage: string = 'basic_land';

    onLoad() {
        this.initEvolutionTree();
    }

    private initEvolutionTree() {
        // 基础陆地生物
        this.addEvolutionNode('basic_land', {
            name: '基础陆地生物',
            description: '适应陆地生活的基础形态',
            requirements: [],
            effects: {
                speed: 5,
                health: 100,
                attack: 10
            },
            skills: ['basic_bite']
        });

        // 敏捷型进化
        this.addEvolutionNode('agile_runner', {
            name: '敏捷奔跑者',
            description: '进化出更强的奔跑能力',
            requirements: ['basic_land'],
            effects: {
                speed: 8,
                stamina: 150,
                agility: 15
            },
            skills: ['sprint', 'quick_dodge']
        });

        // 强壮型进化
        this.addEvolutionNode('heavy_armor', {
            name: '重装防御者',
            description: '发展出坚硬的外壳防御',
            requirements: ['basic_land'],
            effects: {
                defense: 20,
                health: 200,
                weight: 2
            },
            skills: ['shell_defense', 'ground_pound']
        });

        // 捕食者进化
        this.addEvolutionNode('predator', {
            name: '陆地猎手',
            description: '成为顶级掠食者',
            requirements: ['agile_runner'],
            effects: {
                attack: 25,
                critRate: 0.2,
                stealth: 10
            },
            skills: ['ambush', 'fatal_bite']
        });

        // 巨兽进化
        this.addEvolutionNode('titan', {
            name: '陆地泰坦',
            description: '进化为巨大的陆地霸主',
            requirements: ['heavy_armor'],
            effects: {
                size: 2,
                health: 500,
                intimidation: 20
            },
            skills: ['stomp', 'roar']
        });
    }

    private addEvolutionNode(id: string, config: EvolutionNodeConfig) {
        const node = new EvolutionNode(id, config);
        this.evolutionNodes.set(id, node);
    }

    public canEvolve(targetStage: string): boolean {
        const node = this.evolutionNodes.get(targetStage);
        if (!node) return false;

        // 检查是否满足前置条件
        return node.requirements.every(req => {
            const reqNode = this.evolutionNodes.get(req);
            return reqNode?.isUnlocked;
        });
    }

    public evolve(targetStage: string): boolean {
        if (!this.canEvolve(targetStage)) return false;

        const node = this.evolutionNodes.get(targetStage);
        if (!node) return false;

        // 解锁进化节点
        node.unlock();
        this.currentStage = targetStage;

        // 应用进化效果
        this.applyEvolutionEffects(node);

        // 发送进化完成事件
        EventManager.instance.emit(GameEvents.EVOLUTION_COMPLETE, {
            stage: targetStage,
            effects: node.effects
        });

        return true;
    }

    private applyEvolutionEffects(node: EvolutionNode) {
        // 应用属性效果
        const character = this.getComponent('CharacterStats');
        if (character) {
            Object.entries(node.effects).forEach(([stat, value]) => {
                character.modifyStat(stat, value);
            });
        }

        // 解锁技能
        const skillManager = this.getComponent('SkillManager');
        if (skillManager) {
            node.skills.forEach(skillId => {
                skillManager.unlockSkill(skillId);
            });
        }
    }

    public getCurrentStage(): string {
        return this.currentStage;
    }

    public getEvolutionNode(stage: string): EvolutionNode | undefined {
        return this.evolutionNodes.get(stage);
    }
}

interface EvolutionNodeConfig {
    name: string;
    description: string;
    requirements: string[];
    effects: {
        [key: string]: number;
    };
    skills: string[];
} 